package com.example.uit_management;

import java.util.ArrayList;
import java.util.HashMap;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class Activity_ImformationStudent extends Activity {


	/**Biến kiểu JsonParser_ImformationStudent lấy thong tin ve sinh vien*/
	private JsonParser_InformationStudent JSON = new JsonParser_InformationStudent();
	/**Biến kiểu ArrayList<HashMap<String, String>> mang thong tin sinh vien */
	private ArrayList<HashMap<String, String>> ArrayMap = new ArrayList<HashMap<String, String>>(); 
	/**Biến kiểu HashMap<String, String> lay thong tinh sinh vien*/
	private HashMap<String, String> Map = new HashMap<String, String>();
	/**Biến kiểu JsonParser_SCoreStudent lấy thong tin ve diem cua sinh vien*/
	private JsonParser_ScoreStudent Json_scorestudent =new JsonParser_ScoreStudent();
	private ArrayList<HashMap<String, String>> ArrayMap_score = new ArrayList<HashMap<String, String>>(); 
	/** tong diem */
	float Total_score =0;
	/** Tong tinh chi */
	float Total_TCC_full = 0;
	/** Tong tinh chi da co diem */
	float Total_TCC = 0;
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_imformation_student);
        // xóa dialog
        Menu_GridView.dialog.dismiss();
        // lay Json thong tin ve sinh vien
        ArrayMap = JSON.getMap();
        Map = ArrayMap.get(0);
        // Ten sinh vien
        TextView textView_informationStudent = (TextView)findViewById(R.id.textView_informationStudent_Name);
        textView_informationStudent.setText(Map.get(JsonParser_InformationStudent.KEY_studentName));
        // Gioi tinh sinh vien
        textView_informationStudent = (TextView)findViewById(R.id.textView_informationStudent_Gender);
        textView_informationStudent.setText(Map.get(JsonParser_InformationStudent.KEY_studentGender));
        // Ngay thang nam sinh cua sinh vien
        textView_informationStudent = (TextView)findViewById(R.id.textView_informationStudent_BirthDay);
        textView_informationStudent.setText(Map.get(JsonParser_InformationStudent.KEY_studentBirthday));
        // MSSV cua sinh vien
        textView_informationStudent = (TextView)findViewById(R.id.textView_informationStudent_IDstudent);
        textView_informationStudent.setText(Map.get(JsonParser_InformationStudent.KEY_studentID));
        // lop cua sinh vien
        textView_informationStudent = (TextView)findViewById(R.id.textView_informationStudent_Class);
        textView_informationStudent.setText(Map.get(JsonParser_InformationStudent.KEY_classID));
        // Ten khoa cua sinh vien
        textView_informationStudent = (TextView)findViewById(R.id.textView_informationStudent_Faculty);
        textView_informationStudent.setText(Map.get(JsonParser_InformationStudent.KEY_facultyName));
        // Dia chi cua sinh vien
        textView_informationStudent = (TextView)findViewById(R.id.textView_informationStudent_Address);
        textView_informationStudent.setText(Map.get(JsonParser_InformationStudent.KEY_studentAddress));
        // Tong tinh chi cua sinh vien
        textView_informationStudent = (TextView)findViewById(R.id.textView_informationStudent_TotalTC);
        textView_informationStudent.setText("Not found");
        // Tong tinh chi cua sinh vien
        textView_informationStudent = (TextView)findViewById(R.id.textView_informationStudent_TotalScore);
        textView_informationStudent.setText("Not found");
        ArrayMap_score = Json_scorestudent.getMap();
        Get_SetText_total(ArrayMap_score);
    }

	  @Override
	    public boolean onCreateOptionsMenu(Menu menu) {
	        getMenuInflater().inflate(R.menu.activity__score, menu);
	        return true;
	    }  
	    @Override
	    public boolean onOptionsItemSelected(MenuItem item) {
	        switch (item.getItemId()) {
	        case R.id.menu_back:{
	        	finish();  
	        	Intent intent = new Intent(getApplicationContext(),Menu_GridView.class);
					startActivity(intent);				
					break;
	        	}
	        }
	        return true;
	    }
	    private void Get_SetText_total(ArrayList<HashMap<String, String>> array_MAP){
	    	Log.e(Activity_Score.class.toString(),"vao  Get_SetText_total");
	    	Total_TCC =0.00f;
	    	Total_score =0.00f;
	    	Total_TCC_full = 0.00f;
	    	HashMap<String, String> MAP;
	    	Log.e(Activity_Score.class.toString(),"1");
	    	for (int i = 0; i < array_MAP.size(); i++) {
				MAP = array_MAP.get(i);
				Log.e(Activity_Score.class.toString(),"2: "+i);
				if(!MAP.get(JsonParser_ScoreStudent.KEY_scoreFinal).toString().equals("") && !MAP.get(JsonParser_ScoreStudent.KEY_subjectCredit).toString().equals("")){
		        	Total_TCC =  Total_TCC+ Float.parseFloat(MAP.get(JsonParser_ScoreStudent.KEY_subjectCredit).toString());
		        	Log.e(Activity_Score.class.toString(),"2.1: "+i);
					Total_score = Total_score + Float.parseFloat(MAP.get(JsonParser_ScoreStudent.KEY_scoreFinal).toString())*Float.parseFloat(MAP.get(JsonParser_ScoreStudent.KEY_subjectCredit).toString());
		        	Log.e(Activity_Score.class.toString(),"2.2: "+i);
				}
	        	Total_TCC_full =  Total_TCC_full+ Float.parseFloat(MAP.get(JsonParser_ScoreStudent.KEY_subjectCredit).toString());
	        	Log.e(Activity_Score.class.toString(),"i :" +i);
	        	Log.e(Activity_Score.class.toString(),"TC  " +Total_TCC);
	        	Log.e(Activity_Score.class.toString(),"Diem " +Total_score);
	        	Log.e(Activity_Score.class.toString(),"3: "+i);
			} 
	    	Log.e(Activity_Score.class.toString(),"4");
	    	float TB = 0.00f;
	    	Log.e(Activity_Score.class.toString(),"5");
	    	if(Total_TCC >0){
	    	TB = (float)Total_score /(float)Total_TCC;
	    	}else
	    	TB=0;
	    	Log.e(Activity_Score.class.toString(),"6");
	    	TextView TextView_Total_TC = (TextView) findViewById(R.id.textView_informationStudent_TotalTC);
	    	Log.e(Activity_Score.class.toString(),"7");
	    	TextView_Total_TC.setText(String.format("%.0f",Total_TCC));
	    	Log.e(Activity_Score.class.toString(),"8");
	    	TextView TextView_Total_score = (TextView) findViewById(R.id.textView_informationStudent_TotalScore);
	    	Log.e(Activity_Score.class.toString(),"9");
	    	TextView_Total_score.setText(String.format("%.2f", TB));
	    	Log.e(Activity_Score.class.toString(),"10");
	    }
}
